﻿ <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="users.aspx.cs" Inherits="SampleSite.admin.users" %>


    <%-- 模糊搜索 --%>
    <div>
        <span>用户中文名:</span><input type="text" id="SU_CName" name="U_CName" value="" size=10 />
		<a href="#" onclick="FindData()" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
    </div>

    <%-- users --%>
    <div style="margin-top:5px;">  
        <table id="users" style="height:auto"></table>
    </div>

    <%-- 添加用户 --%>
    <div id="dlgAddUser" class="easyui-dialog" style="width:340px;height:410px;padding:10px 20px"
			closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
        <form id="AddUser" method="post" novalidate="novalidate">
            <table>
                <tr>
                    <th colspan="2" >添加用户</th>
                </tr>
                <tr>
                    <td><label for="AddU_LoginNO">登录名：</label></td>
                    <td>
                        <input class="easyui-validatebox" type="text" id="AddU_LoginNO" name="U_LoginNO" 
                               style="width:175px;"
                               data-options="required:true,validType:'unnormal'" /></td>
                </tr>                
                <tr>
                    <td><label for="AddU_CName">用户中文名：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="AddU_CName" name="U_CName" 
                               style="width:175px;"
                               data-options="required:true,validType:['chinese']" /></td>
                </tr>
                <tr>
                    <td><label for="AddU_EName">用户英文名：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="AddU_EName" name="U_EName" 
                               style="width:175px;"
                               data-options="required:true,validType:'english'" /></td>
                </tr>
                <tr>
                    <td><label for="AddU_IDCard">身份证号：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="AddU_IDCard" name="U_IDCard" 
                               style="width:175px;"
                               data-options="required:true,validType:'idcard'" /></td>
                </tr>
                <tr>
                    <td><label for="AddU_Sex">性别：</label></td>
                    <td><select id="AddU_Sex" name="U_Sex" editable="false" class="easyui-combobox" style="width:50px" >
		                    <option value="1">男</option>
		                    <option value="2">女</option>
                        </select></td>
                </tr>
                <tr>
                    <td><label for="AddU_BirthDay">出生日期：</label></td>
                    <td><input id="AddU_BirthDay" name="U_BirthDay" type="text" class="easyui-datebox" required="required" /></td>
                </tr>
                <tr>
                    <td><label for="AddU_HomeTel">电话：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="AddU_HomeTel" name="U_HomeTel" 
                               style="width:175px;"
                               data-options="required:true,validType:'phone'" /></td>
                </tr>
                <tr>
                    <td><label for="AddU_MobileNo">手机：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="AddU_MobileNo" name="U_MobileNo" 
                               style="width:175px;"
                               data-options="required:true,validType:'mobile'" /></td>
                </tr>
                <tr>
                    <td><label for="AddU_Email">个人邮箱：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="AddU_Email" name="U_Email" 
                               style="width:175px;"
                               data-options="required:true,validType:'email'" /></td>
                </tr>
                <tr>
                    <td><label for="AddU_CompanyMail">公司邮箱：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="AddU_CompanyMail" name="U_CompanyMail" 
                               style="width:175px;"
                               data-options="required:true,validType:'email'" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:center; padding-top:10px">
                        <a href="javascript:void(0)" 
                           class="easyui-linkbutton" 
                           id="btnAddUser" 
                           iconcls="icon-ok" >确定</a>
                        <a href="javascript:void(0)" 
                           class="easyui-linkbutton close_dlg"
                           dlg="dlgAddUser"
                           iconcls="icon-cancel">关闭</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    
    <%-- 修改用户 --%>
    <div id="dlgUpdateUser" class="easyui-dialog" style="width:340px;height:410px;padding:10px 20px"
			closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
        <form id="UpdateUser" method="post" novalidate="novalidate">
            <table>
                <tr>
                    <th colspan="2" >修改用户</th>
                </tr>
                <tr>
                    <td><label for="UpdateU_LoginNO">登录名：</label></td>
                    <td>
                        <input class="easyui-validatebox" type="text" id="UpdateU_LoginNO" name="U_LoginNO" 
                               style="width:175px;" readOnly="true" disabled="disabled"
                               data-options="required:true,validType:'unnormal'" />
                        <input type="hidden" id="UpdateU_ID" name="U_ID" />
                    </td>
                </tr>                
                <tr>
                    <td><label for="UpdateU_CName">用户中文名：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="UpdateU_CName" name="U_CName" 
                               style="width:175px;"
                               data-options="required:true,validType:['chinese']" /></td>
                </tr>
                <tr>
                    <td><label for="UpdateU_EName">用户英文名：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="UpdateU_EName" name="U_EName" 
                               style="width:175px;"
                               data-options="required:true,validType:'english'" /></td>
                </tr>
                <tr>
                    <td><label for="UpdateU_IDCard">身份证号：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="UpdateU_IDCard" name="U_IDCard" 
                               style="width:175px;"
                               data-options="required:true,validType:'idcard'" /></td>
                </tr>
                <tr>
                    <td><label for="UpdateU_Sex">性别：</label></td>
                    <td><select id="UpdateU_Sex" name="U_Sex" editable="false" class="easyui-combobox" style="width:50px" >
		                    <option value="1">男</option>
		                    <option value="2">女</option>
                        </select></td>
                </tr>
                <tr>
                    <td><label for="UpdateU_BirthDay">出生日期：</label></td>
                    <td><input id="UpdateU_BirthDay" name="U_BirthDay" type="text" class="easyui-datebox" required="required" /></td>
                </tr>
                <tr>
                    <td><label for="UpdateU_HomeTel">电话：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="UpdateU_HomeTel" name="U_HomeTel" 
                               style="width:175px;"
                               data-options="required:true,validType:'phone'" /></td>
                </tr>
                <tr>
                    <td><label for="UpdateU_MobileNo">手机：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="UpdateU_MobileNo" name="U_MobileNo" 
                               style="width:175px;"
                               data-options="required:true,validType:'mobile'" /></td>
                </tr>
                <tr>
                    <td><label for="UpdateU_Email">个人邮箱：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="UpdateU_Email" name="U_Email" 
                               style="width:175px;"
                               data-options="required:true,validType:'email'" /></td>
                </tr>
                <tr>
                    <td><label for="UpdateU_CompanyMail">公司邮箱：</label></td>
                    <td><input class="easyui-validatebox" type="text" id="UpdateU_CompanyMail" name="U_CompanyMail" 
                               style="width:175px;"
                               data-options="required:true,validType:'email'" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:center; padding-top:10px">
                        <a href="javascript:void(0)" 
                           class="easyui-linkbutton" 
                           id="btnUpdateUser" 
                           iconcls="icon-ok" >确定</a>
                        <a href="javascript:void(0)" 
                           class="easyui-linkbutton close_dlg"
                           dlg="dlgUpdateUser"
                           iconcls="icon-cancel">关闭</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>

    <%-- 启用禁用 --%>
    <div id="dlgUpState" class="easyui-dialog" style="width:330px;height:360px;padding:10px 20px"
			closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
        <form id="UpState" method="post" novalidate="novalidate">
            <input type="hidden" id="UpU_ID" name="U_ID" />
            <input type="hidden" id="UpU_State" name="U_State" />
        </form>
    </div>

    <%-- 修改职位 --%>
    <div id="dlgUpPosition" class="easyui-dialog" style="width:800px;height:600px;padding:10px 20px"
            closed="true" zesizable="true" modal="true" buttons="#dlg-buttons" align="center">
        <form id="UpPosition" method="post" novalidate="novalidate">
            <div style="float:left;">
                <table id="orgGrid" style="width: 370px;height:540px;"></table>
            </div>
            <div style="float:left;margin-left:10px;">
                <table id="positions" class="easyui-datagrid" style="width:340px;height:200px;">
                </table>
            </div>
            <div style="float:left;margin-left:185px;margin-top:10px;">
                <a href="javascript:void(0)" 
                           class="easyui-linkbutton" 
                           id="btnUpUserPositions" 
                           iconcls="icon-add" >确定</a>
                <a href="javascript:void(0)" 
                           class="easyui-linkbutton close_dlg" 
                           dlg="dlgUpPosition" 
                           iconcls="icon-remove" >关闭</a>
            </div>
        </form>
    </div>

<%-- 修改用户角色 --%>
<div id="dlgUpUserRole" class="easyui-dialog" style="width: 450px; height: 450px; padding: 10px 20px"
    closed="true" resizable="true" modal="true" buttons="#dlg-buttons" align="center">
    <form id="UpUserRole" method="post" novalidate="novalidate">
        <div style="float: left; margin-left: 10px;">
            <table id="Roles" class="easyui-datagrid" style="width: 380px; height: 335px;">
            </table>
        </div>
        <div style="float: left; margin-left: 120px; margin-top: 10px;">
            <a href="javascript:void(0)"
                class="easyui-linkbutton"
                id="btnUpUserRole"
                iconcls="icon-add">确定</a>
            <a href="javascript:void(0)"
                class="easyui-linkbutton close_dlg"
                dlg="dlgUpUserRole"
                iconcls="icon-remove">关闭</a>
        </div>
    </form>
</div>

    <%-- 初始化users函数 --%>
    <script type="text/ecmascript">
        var lastExpandRow = -1;

        function loadUserPosition(u_id) {
            var grid = $('#userPosition_' + u_id);
            grid.datagrid({
                title: '用户职位',
                rownumbers: true,
                singleSelect: true,
                idField: 'U_ID',
                remoteSort: false,
                url: '/admin/ajax/getUserPosition.ashx?U_ID=' + u_id,

                columns: [[
                    { field: 'U_ID', hidden: true },
                    { field: 'P_State', width: 50, title: "状态" },
                    { field: 'IsMain', width: 80, title: '是否主职位' },
                    { field: 'O_CName', width: 100, title: '部门中文名' },
                    { field: 'O_EName', width: 100, align: 'right', title: '部门英文名' },
                    { field: 'P_CName', width: 100, title: '职位中文名' },
                    { field: 'P_EName', width: 100, align: 'right', title: '职位英文名' }
                ]],

                toolbar: [{

                    id: 'btnMain',
                    text: '设为主要职位',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var prow = $('#userPosition_' + u_id).datagrid('getSelections');
                        if (prow) {
                            if (prow[0].IsMain == '次要') {
                                var msg = '是否设为主要职位?';
                                $.messager.confirm(
                                    '提示',
                                    msg,
                                    function (result) {
                                        if (result == false) {
                                            return;
                                        }

                                        $.post('/admin/ajax/mainUserPosition.ashx',
                                        { U_ID: prow[0].U_ID, P_ID: prow[0].P_ID },
                                        function (data) {
                                            if (data.state == 'ok') {
                                                $.messager.alert('提示', '操作成功');
                                                //$('#users').datagrid('reload');

                                                $('#userPosition_' + u_id).datagrid('clearSelections');
                                                $('#userPosition_' + u_id).datagrid('reload');
                                            }
                                            else {
                                                $.messager.alert('提示', '操作失败');
                                            }

                                        }, 'json');

                                    });
                            }
                            else {
                                $.messager.alert('提示', '该用户职位已为主要职位！');
                            }
                        }
                    }
                }, '-', {
                    id: 'btnState',
                    text: '启用/禁用',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var row = $('#userPosition_' + u_id).datagrid('getSelections');
                        if (row) {
                            var msg = row[0].P_State == '禁用' ?
                                '是否启用当前选择用户职位?' :
                                '是否禁用当前选择用户职位?';
                            $.messager.confirm(
                                '提示',
                                msg,
                                function (result) {
                                    if (result == false) {
                                        return;
                                    }

                                    $.post('/admin/ajax/enableUserPosition.ashx',
                                        { U_ID: row[0].U_ID, P_ID: row[0].P_ID, P_State: row[0].P_State == '禁用' ? 0 : 1 },
                                        function (data) {
                                            if (data.state == 'ok') {
                                                $.messager.alert('提示', '操作成功');
                                                //$('#users').datagrid('reload');

                                                $('#userPosition_' + u_id).datagrid('clearSelections');
                                                $('#userPosition_' + u_id).datagrid('reload');
                                            }
                                            else {
                                                $.messager.alert('提示', '操作失败');
                                            }

                                        }, 'json');
                                });
                        }
                    }
                }]
            });

            grid.datagrid('clearSelections');
        }

        function loadRoles(u_id) {
            // TODO: 改ID名
            var grid = $('#userRole_' + u_id);
            grid.datagrid({
                title: '用户角色',
                rownumbers: true,
                singleSelect: true,
                idField: 'U_ID',
                remoteSort: false,
                url: '/admin/ajax/getUserRole.ashx?U_ID=' + u_id,

                columns: [[
                    { field: 'U_ID', hidden: true },
                    { field: 'R_ID', hidden: true },
                    { field: 'R_State', width: 50, title: "状态" ,formatter: function (v, r, i) { if (v == 1) return '启用'; else return '禁用'; }},
                    { field: 'R_CName', width: 100, title: '角色中文名' },
                    { field: 'R_EName', width: 100, align: 'right', title: '角色英文名' },
                    { field: 'R_Description', width: 100, title: '角色说明' }
                ]]
            });
            grid.datagrid('clearSelections');
        }

        function init_users() {
            // 初始化users
            $('#users').datagrid({
                url: '/admin/ajax/getUsers.ashx',
                title: '用户管理',
                rownumbers: true,
                pagination: true,
                idField: 'U_ID',
                singleSelect: true,
                view: detailview,

                columns: [[
                    { field: 'U_ID', hidden: true },
                    { field: 'U_State', width: 50, title: "状态" },
                    { field: 'U_LoginNO', width: 100, title: '登录名' },
                    { field: 'U_CName', width: 100, title: '用户中文名' },
                    { field: 'U_EName', width: 100, align: 'right', title: '用户英文名' },
                    { field: 'U_IDCard', width: 130, align: 'right', title: '身份证号' },
                    { field: 'U_Sex', width: 40, align: 'right', title: '性别' },
                    { field: 'U_BirthDay', width: 80, align: 'right', title: '出生日期' },
                    { field: 'U_HomeTel', width: 80, align: 'right', title: '电话' },
                    { field: 'U_MobileNo', width: 80, align: 'right', title: '手机' },
                    { field: 'U_Email', width: 145, align: 'right', title: '个人邮箱' },
                    { field: 'U_CompanyMail', width: 145, align: 'right', title: '公司邮箱' }
                ]],

                onClickRow: function (i, r) {
                    $('#users').datagrid('expandRow', i);
                },

                detailFormatter: function (i, r) {
                    return '<div style="height:370px; padding:10px;">' +
                           '<div style="height:180px;">' +
                           '<table id="userPosition_' + r.U_ID + '" style="height:180px;"></table>' +
                           '<div>' +
                           '<div style="height:180px;margin-top:10px;">' +
                           // TODO: 改ID名
                           '<table id="userRole_' + r.U_ID + '" style="height:180px;"></table>' +
                           '</div>' +
                           '</div>';
                },

                onExpandRow: function (i, r) {
                    if (lastExpandRow != -1 && lastExpandRow != i) {
                        $('#users').datagrid('collapseRow', lastExpandRow);
                    }

                    loadUserPosition(r.U_ID);

                    // TODO: 改函数名
                    loadRoles(r.U_ID);

                    lastExpandRow = i;
                },

                // 分隔符
                toolbar: [{
                    id: '_btnAddUser',
                    text: '添加用户',
                    iconCls: 'icon-add',
                    handler: function () {
                        dlgAddUser.showAddUserDialog();
                    }
                }, '-', {
                    id: 'btncut',
                    text: '修改',
                    iconCls: 'icon-cut',
                    handler: function () {
                        var rows = $('#users').datagrid('getSelections');
                        if (rows.length < 1) {
                            $.messager.alert('提示', '请选择要修改的用户！');
                        }
                        else {
                            dlgUpdateUser.showUpdateUserDialog();
                        }
                    }
                }, '-', {
                    id: 'btnsave',
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        onDelete();
                    }
                }, '-', {
                    id: 'btnState',
                    text: '启用/禁用',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var row = $('#users').datagrid('getSelections');
                        if (row) {
                            var msg = row[0].U_State == '禁用' ?
                                '是否启用当前选择用户?' :
                                '是否禁用当前选择用户?';
                            $.messager.confirm(
                                '提示',
                                msg,
                                function (r) {
                                    if (r == false) {
                                        return;
                                    }

                                    dlgUpState.ToUpState();
                                });
                        }
                    }
                }, '-', {
                    id: 'btnUpPosition',
                    text: '修改职位',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var rows = $('#users').datagrid('getSelections');
                        if (rows.length < 1) {
                            $.messager.alert('提示', '请选择要修改的用户！');
                        }
                        else {
                            positionsData = null;
                            dlgUpPosition.showUpUserPositionDialog();
                            userpost.getUserPost();
                        }
                    }
                }, '-', {
                    id: 'btnUpPosiRole',
                    text: '修改用户角色',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var rows = $('#users').datagrid('getSelections');
                        if (rows.length < 1) {
                            $.messager.alert('提示', '请选择要修改的职位！');
                        }
                        else {
                            $('#Roles').datagrid('options').url = '/admin/ajax/getRoleByUser.ashx';
                            $('#Roles').datagrid('reload', {
                                U_ID: rows[0].U_ID
                            });
                            dlgUpUserRole.showUserRoleDialog();
                        }
                    }
                }]
            });
        }

        function FindData() {
            var uname = $('#SU_CName').val();
            $('#users').datagrid('reload', { "U_CName": uname });
        }
    </script>

    <%-- 初始化修改用户角色窗口 --%>
    <script type="text/javascript">
        var dlgUpUserRole = {
            clearUserRoleDialog: function () {

            },

            showUserRoleDialog: function () {
                dlgUpUserRole.clearUserRoleDialog();
                $('#dlgUpUserRole').dialog('open');
            },

            updateUserRole: function () {
                var validate = $('#UpUserRole').form('validate');
                if (validate == false) {
                    return false;
                }

                var row = $('#users').datagrid('getSelected');
                var row1 = $('#Roles').datagrid('getSelections');
                var myjson = null;
                if (row1.length > 0) {
                    var mselect = { rows: [{ U_ID: row.U_ID, R_ID: row1[0].R_ID }] };
                    for (var i = 1; i < row1.length; i++) {
                        mselect.rows.push({ U_ID: row.U_ID, R_ID: row1[i].R_ID });
                    }
                }
                else {
                    var mselect = { rows: [{ U_ID: row.U_ID, R_ID: 0 }] };
                }

                myjson = JSON.stringify(mselect.rows);

                $.post('/admin/ajax/updateUserRole.ashx', { data: myjson }, function (data) {
                    if (data.state == 'ok') {
                        $.messager.alert('提示', '修改成功');

                        $('#dlgUpUserRole').dialog('close');
                        $('#userRole_' + row.U_ID).datagrid('reload');
                    }
                    else {
                        $.messager.alert('提示', '修改失败:' + data.msg);
                    }
                }, 'json');

            },

            init: function () {
                $('#btnUpUserRole').click(dlgUpUserRole.updateUserRole);
                $('#dlgUpUserRole').dialog({ title: '选择角色' });
            }
        };
    </script>

    <%-- 初始化添加用户窗口 --%>
    <script type="text/ecmascript">
        var dlgAddUser = {};

        dlgAddUser.clearAddUserDialog = function () {
            $('#AddU_LoginNO').val('');
            $('#AddU_CName').val('');
            $('#AddU_EName').val('');
            $('#AddU_IDCard').val('');
            $('#AddU_Sex').combobox('setValue', 1);
            $('#AddU_BirthDay').datebox('setValue', '');
            $('#AddU_HomeTel').val('');
            $('#AddU_MobileNo').val('');
            $('#AddU_Email').val('');
            $('#AddU_CompanyMail').val('');
        };

        dlgAddUser.showAddUserDialog = function () {
            dlgAddUser.clearAddUserDialog();
            $('#dlgAddUser').dialog('open');
        };

        dlgAddUser.addUser = function () {
            var validate = $('#AddUser').form('validate');
            if (validate == false) {
                return false;
            }

            $.post('/admin/ajax/addUser.ashx', $('#AddUser').serialize(), function (data) {
                if (data.state == 'ok') {
                    $.messager.alert('提示', '添加成功');

                    $('#dlgAddUser').dialog('close');
                    $('#users').datagrid('reload');
                }
                else {
                    $.messager.alert('提示', '添加失败:' + data.msg);
                }
            }, 'json');
        };

        dlgAddUser.init = function () {
            $('#btnAddUser').click(dlgAddUser.addUser);
            $('#dlgAddUser').dialog({ title: '添加用户' });
        };
    </script>

    <%-- 初始化添加用户窗口 --%>
    <script type="text/ecmascript">
        var dlgUpdateUser = {
            clearUpdateUserDialog: function () {
                $('#UpdateU_LoginNO').val('');
                $('#UpdateU_CName').val('');
                $('#UpdateU_EName').val('');
                $('#UpdateU_IDCard').val('');
                $('#UpdateU_Sex').combobox('setValue', 1);
                $('#UpdateU_BirthDay').datebox('setValue', '');
                $('#UpdateU_HomeTel').val('');
                $('#UpdateU_MobileNo').val('');
                $('#UpdateU_Email').val('');
                $('#UpdateU_CompanyMail').val('');
            },

            UpdateUserInit: function () {
                var rows = $('#users').datagrid('getSelections');
                $('#UpdateU_ID').val(rows[0].U_ID);
                $('#UpdateU_LoginNO').val(rows[0].U_LoginNO);
                $('#UpdateU_CName').val(rows[0].U_CName);
                $('#UpdateU_EName').val(rows[0].U_EName);
                $('#UpdateU_IDCard').val(rows[0].U_IDCard);
                $('#UpdateU_Sex').combobox('setValue', rows[0].U_Sex == '男' ? 1 : 2);
                $('#UpdateU_BirthDay').datebox('setValue', rows[0].U_BirthDay);
                $('#UpdateU_HomeTel').val(rows[0].U_HomeTel);
                $('#UpdateU_MobileNo').val(rows[0].U_MobileNo);
                $('#UpdateU_Email').val(rows[0].U_Email);
                $('#UpdateU_CompanyMail').val(rows[0].U_CompanyMail);
            },

            showUpdateUserDialog: function () {
                dlgUpdateUser.clearUpdateUserDialog();
                dlgUpdateUser.UpdateUserInit();
                $('#dlgUpdateUser').dialog('open');
            },

            UpdateUser: function () {
                var validate = $('#UpdateUser').form('validate');
                if (validate == false) {
                    return false;
                }

                $.post('/admin/ajax/updateUser.ashx', $('#UpdateUser').serialize(), function (data) {
                    if (data.state == 'ok') {
                        $.messager.alert('提示', '修改成功');

                        $('#dlgUpdateUser').dialog('close');
                        $('#users').datagrid('reload');
                    }
                    else {
                        $.messager.alert('提示', '修改失败:' + data.msg);
                        //$.messager.alert('错误', data.msg);
                    }
                }, 'json');
            },

            init: function () {
                $('#btnUpdateUser').click(dlgUpdateUser.UpdateUser);
                $('#dlgUpdateUser').dialog({ title: '修改用户' });
            }
        };
    </script>
    
    <%-- 初始化启用禁用窗口 --%>
    <script type="text/javascript">
        var dlgUpState = {
            clearUpStateDialog: function () {
                $('#UpU_ID').val('');
                $('#UpU_State').val('');
            },

            UpStateInit: function () {
                var rows = $('#users').datagrid('getSelections');
                $('#UpU_ID').val(rows[0].U_ID);
                $('#UpU_State').val(rows[0].U_State == '启用' ? 1 : 0);
            },

            ToUpState: function () {
                dlgUpState.clearUpStateDialog();
                dlgUpState.UpStateInit();
                dlgUpState.UpState();
            },

            UpState: function () {
                var validate = $('#UpState').form('validate');
                if (validate == false) {
                    return false;
                }

                $.post('/admin/ajax/updateUserState.ashx', $('#UpState').serialize(), function (data) {
                    if (data.state == 'ok') {
                        $.messager.alert('提示', '操作成功');
                        $('#users').datagrid('reload');
                    }
                    else {
                        $.messager.alert('提示', '操作失败:' + data.msg);
                        //$.messager.alert('错误', data.msg);
                    }
                }, 'json');
            },

            init: function () {
                $('#btnUpdateUser').click(dlgUpdateUser.UpdateUser);
                $('#dlgUpdateUser').dialog({ title: '修改职位' });
            }
        };
    </script>

    <%-- 初始化修改职位相关grid --%>
    <script type="text/javascript">
        var positionsData;

        var userpost = {
            getUserPost: function () {
                var row = $('#users').datagrid('getSelections');
                $.post('/admin/ajax/getUserPositionsByUser.ashx', { U_ID: row[0].U_ID }, function (data) {
                    positionsData = data;
                }, 'json')
            }
        }

        function init_Org() {
            $('#orgGrid').treegrid(
                {
                    title: '选择组织架构',
                    rownumbers: true,
                    idField: 'id',
                    treeField: 'name',
                    columns: [[
                        { title: '中文名称', field: 'name', width: 240, sortable: false }
                    ]],

                    onClickRow: function () {
                        $('#positions').datagrid('options').url = '/admin/ajax/getPositionsByOrg.ashx';

                        var row = $('#orgGrid').treegrid('getSelected');
                        var row1 = $('#users').datagrid('getSelected');

                        if (row && row1) {
                            $('#positions').datagrid('reload', {
                                O_ID: row.id,
                                U_ID: row1.U_ID
                            });
                        }
                    },
                });
        }

        function init_Positions() {
            $('#positions').datagrid(
            {
                idField: 'P_ID',
                rownumbers: true,
                title: '选择职位',  //标识

                columns: [[
                    { field: 'ck', checkbox: true },
                    { field: 'P_ID', hidden: true },
                    { field: 'P_Org', hidden: true },
                    { field: 'IsChecked', hidden: true },
                    { field: 'P_Level', hidden: true },
                    { field: 'P_CName', width: 100, title: '职位中文名' },
                    { field: 'P_EName', width: 100, title: '职位英文名' }
                ]],

                onClickRow: function () {
                },
                onLoadSuccess: function (data) {
                    $('#positions').datagrid('uncheckAll');
                    for (var i = 0; i < data.rows.length; i++) {
                        if (data.rows[i].IsChecked)
                            $('#positions').datagrid('selectRecord', data.rows[i].P_ID);
                    }
                },
                onCheck: function (rowIndex, rowData) {
                    var row = $('#users').datagrid('getSelections');
                    for (var i = 0; i < positionsData.rows.length; i++) {
                        if (positionsData.rows[i].P_ID == rowData.P_ID) {
                            positionsData.rows[i].P_State = 1;
                            return;
                        }
                    }

                    positionsData.rows.push({
                        U_ID: row[0].U_ID,
                        P_ID: rowData.P_ID,
                        IsMain: positionsData.rows.length == 0 ? true : false,
                        P_State: 1
                    });

                },
                onUncheck: function (rowIndex, rowData) {
                    var row = $('#users').datagrid('getSelections');
                    for (var i = 0; i < positionsData.rows.length; i++) {
                        if (positionsData.rows[i].P_ID == rowData.P_ID) {
                            positionsData.rows[i].P_State = 2;
                            return;
                        }
                    }
                }
            });
        }
    </script>

    <%-- 初始化修改职位窗口 --%>
    <script type="text/javascript">
        var dlgUpPosition = {
            clearUpPositionDialog: function () {
                $('#orgGrid').treegrid('loadData', []);
                $('#positions').datagrid('loadData', []);
                positionsData = {};
            },

            UpPositionInit: function () {
                $('#orgGrid').treegrid('options').url = '/admin/ajax/getOrg.ashx';
                $('#orgGrid').treegrid('reload');
            },

            showUpUserPositionDialog: function () {
                dlgUpPosition.clearUpPositionDialog();
                dlgUpPosition.UpPositionInit();
                $('#dlgUpPosition').dialog('open');
            },

            UpPosition: function () {
                var validate = $('#UpPosition').form('validate');
                if (validate == false) {
                    return false;
                }

                if (positionsData.rows.length > 0) {
                    var myjson = JSON.stringify(positionsData.rows);

                    $.post('/admin/ajax/updateUserPosition.ashx', { data: myjson }, function (data) {
                        if (data.state == 'ok') {
                            $.messager.alert('提示', '操作成功');

                            $('#dlgUpPosition').dialog('close');
                            var row = $('#users').datagrid('getSelected');
                            $('#userPosition_' + row.U_ID).datagrid('reload');
                        }
                        else {
                            $.messager.alert('提示', '操作失败:' + data.msg);
                            //$.messager.alert('错误', data.msg);
                        }
                    }, 'json');
                }
                else {
                    $.messager.alert('提示', '未指定任何职位！');
                }
            },
            init: function () {
                $('#btnUpUserPositions').click(dlgUpPosition.UpPosition);
                $('#dlgUpPosition').dialog({ title: '选择职位' });
            }
        };

        function init_Roles() {
            $('#Roles').datagrid(
                {
                    title: '选择角色',
                    rownumbers: true,
                    idField: 'R_ID',
                    columns: [[
                        { field: 'ck', checkbox: true },
                        { field: 'IsHave', hidden: true },
                        { title: '角色中文名', field: 'R_CName', width: 100, sortable: false },
                        { title: '角色英文名', field: 'R_EName', width: 100, sortable: false },
                        { title: '角色说明', field: 'R_Description', width: 100, sortable: false }
                    ]],

                    onLoadSuccess: function (data) {
                        $('#Roles').datagrid('uncheckAll');
                        for (var i = 0; i < data.rows.length; i++) {
                            if (data.rows[i].IsHave == 1)
                                $('#Roles').datagrid('selectRecord', data.rows[i].R_ID);
                        }
                    },
                });
        }

        function onDelete() {
            var row = $('#users').datagrid('getSelected');
            if (row) {
                $.messager.confirm(
                    "提示", "是否删除当前选择项?",
                    function (r) {
                        if (r == true) {
                            $.post(
                                '/admin/ajax/deleteUser.ashx',
                                {
                                    Id: row.U_ID
                                },
                                function (data) {
                                    if (data.state == 'ok') {
                                        $.messager.alert('提示', '删除成功');
                                        $('#users').datagrid('reload');
                                    }
                                    else
                                        $.messager.alert('提示', '删除失败');
                                }, 'json');
                        }
                    });
            }
            else {
                $.messager.alert('提示', '请先选择需要删除的用户');
            }
        }
    </script>

    <%-- document.ready --%>
    <script type="text/javascript">
        $(document).ready(function () {
            init_users();
            init_Org();
            init_Positions();
            init_Roles();

            dlgAddUser.init();
            dlgUpdateUser.init();
            dlgUpState.init();
            dlgUpPosition.init();
            dlgUpUserRole.init();

            // 绑定各个dialog的关闭事件
            $('.close_dlg').each(function (i, e) {
                var dlg = $(e).attr('dlg');
                $(e).click(function () {
                    $('#' + dlg).dialog('close');
                });
            });
        });
    </script>
